package com.zzyl.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.base.PageResponse;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.User;
import com.zzyl.mapper.NursingProjectMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.mapper.UserMapper;
import com.zzyl.service.NursingProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 护理项目表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-15
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements NursingProjectService {

    @Autowired
    private UserMapper userMapper;
    //条件分页查询
    @Override
    public PageResponse<NursingProject> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //设置分页参数
        Page<NursingProject> page = new Page<>(pageNum, pageSize);

        //创建条件查询构造器
        LambdaQueryWrapper<NursingProject> wrapper = new LambdaQueryWrapper<>();
        //设置查询条件 护理项目名称的模糊查询和状态的精确查询
        wrapper.like(StrUtil.isNotEmpty(name),NursingProject::getName,name)
                .eq(status!=null,NursingProject::getStatus,status);

        //进行查询  将查询结果交给page对象
        page = getBaseMapper().selectPage(page, wrapper);

        //查询结果只有创建人id 没有创建人  在User表中查询创建人
        page.getRecords().stream().forEach(nursingProject -> {
            User user = userMapper.selectById(nursingProject.getCreateBy());
            if (user!=null){
                nursingProject.setCreator(user.getRealName());
            }
        });


        return new PageResponse<>(page);
    }
}
